﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DatabaseAccess;
using Liguo.AppCode;
using System.Text.RegularExpressions;
using System.Text;

namespace Liguo.Store
{
    public partial class Detail : System.Web.UI.Page
    {
        public String goodsinfo;
        static String id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                goodsinfo = JsonConvert.GetGoodsInfoJson(((User)Session["User"]).UserID);
            }
            if (!Page.IsPostBack)
            {
                DataSet a = new DataSet();
                SqlIt b = new SqlIt();
                String ID = Request.QueryString["ID"];
                id = ID;
                if (id == null) { Response.Redirect("Store.aspx"); }
                a = b.SpecialItem("GoodsInfo", "ID='" + ID + "'", "*");
                //a = b.SpecialItem("GoodsInfo", "ID=8", "*");
                Goodsnamevalue.Text = a.Tables[0].Rows[0][1].ToString();
                if (a.Tables[0].Rows[0][9].ToString() == "3" || a.Tables[0].Rows[0][9].ToString() == "0")
                {
                    Pricevalue.Text = Convert.ToDouble(a.Tables[0].Rows[0][4]).ToString("F2");
                    value2.Text = "元/件";
                }
                else
                {
                    Pricevalue.Text = Convert.ToDouble(a.Tables[0].Rows[0][4]).ToString("F2");
                    value2.Text = "元/千克";
                }
                if (a.Tables[0].Rows[0][9].ToString() == "3" || a.Tables[0].Rows[0][9].ToString() == "0")
                {
                    WEIGHT1.Visible = true;
                    AMOUNT.Visible = false;
                    Storgevalue.Text = a.Tables[0].Rows[0][6].ToString() + "件";
                    value1.Text = "件";
                }
                else
                {
                    Storgevalue.Text = a.Tables[0].Rows[0][5].ToString() + "千克";
                    value1.Text = "千克";
                    WEIGHT1.Visible = false;
                    AMOUNT.Visible = true;
                }
                if (a.Tables[0].Rows[0][9].ToString() == "3" || a.Tables[0].Rows[0][9].ToString() == "0")
                    SalesValue.Text = a.Tables[0].Rows[0][8].ToString() + "件";
                else SalesValue.Text = a.Tables[0].Rows[0][7].ToString() + "千克";
                GoodsIntroduceValue.Text = a.Tables[0].Rows[0][11].ToString();
                Manufacturers.Text = a.Tables[0].Rows[0][12].ToString();
                GuaranteePeriod.Text = a.Tables[0].Rows[0][13].ToString();
                Weight.Text = a.Tables[0].Rows[0][14].ToString() + "千克";
                String temp = a.Tables[0].Rows[0][10].ToString();
                Image1.ImageUrl = ("~/images/" + temp + ".png");
                value.Text = "1";
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (value.Text == "")
            {

                Response.Write("<script>alert('请输入')</script>");
                return;
            }

            if (Regex.IsMatch(value.Text, @"^-?\d+$") || (Regex.IsMatch(value.Text, "^([0-9]{1,}[.][0-9]*)$")))
            {

                if (Convert.ToDouble(value.Text) > 1000)
                {

                    Response.Write("<script>alert('输入出错')</script>");
                    return;
                }
                if (Convert.ToDouble(value.Text) <= 0)
                {

                    Response.Write("<script>alert('输入出错')</script>");
                    return;
                }
                if (Session["User"] == null)
                {


                    Response.Write("<script>top.location=\"Port.aspx?mod=Login\";</script>");

                }
                else
                {
                    User user = (User)Session["User"];
                    int userid = user.UserID;
                    DataSet a1 = new DataSet();
                    SqlIt b1 = new SqlIt();
                    a1 = b1.SpecialItem("GoodsInfo", "ID='" + id + "'", "*");
                    Dictionary<string, string> myDic = new Dictionary<string, string>();

                    myDic["UserID"] = Convert.ToString(userid);
                    myDic["GoodsID"] = id;
                    if (a1.Tables[0].Rows[0][9].ToString() == "0" || a1.Tables[0].Rows[0][9].ToString() == "0")
                    {
                        if (Regex.IsMatch(value.Text, "^([0-9]{1,}[.][0-9]*)$"))
                        {
                            Response.Write("<script>alert('输入出错')</script>");
                            return;
                        }
                        int t = Convert.ToInt32(value.Text) / 1;
                        value.Text = Convert.ToString(t);
                        myDic["Amount"] = value.Text;
                    }


                    else {

                        
                        myDic["Weight"] = value.Text;
                    }
                    b1.AddMess("CartInfo", myDic);
                    Response.Write("<script type='text/javascript'>alert('成功！');</script>");
                    goodsinfo = JsonConvert.GetGoodsInfoJson(((User)Session["User"]).UserID);
                }

            }
            else
            {
                Response.Write("<script>alert('请输入数字')</script>");
                return;
            }

            
        }
    }
}